//
//  ByMyVC.m
//  hutouben_ios //测试
//
//  Created by mc on 16/11/29.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByMyVC.h"

// vc
#import "ByMineSettingVC.h"                 // 设置

// view
#import "ByMineHeaderView.h"                // 头视图
#import "ByMineLogoutHeaderView.h"          // 未登录头视图

@interface ByMyVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) UIView *showHeader;
@property (nonatomic, copy) NSArray *subjects;
@property (nonatomic, strong) ByMineHeaderView *headerView;
@property (nonatomic, strong) ByMineLogoutHeaderView *logoutHV;
@property (nonatomic, assign) BOOL isLogin;                         // 是否登入
@end

@implementation ByMyVC
{
    NSArray *_titles;
    NSArray *_images;
}

- (UIView *)headerView {
    if (!_headerView) {
        _headerView = [[ByMineHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.showHeader.height)];
    }
    return _headerView;
}
- (UIView *)logoutHV {
    if (!_logoutHV) {
        _logoutHV = [[ByMineLogoutHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.showHeader.height)];
    }
    return _logoutHV;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人中心";
    
    _titles = @[@[@"我的订单", @"维修记录", @"地址管理",@"我的钱包",@"邀请好友"]];
    _images = @[@[@"mine_dingdan", @"mine_weixiuRecord", @"mine_dizhi", @"mine_qianbao",@"mine_invite"]];
    
    [self addBYItemviews:@[@"mine_dizhi"] mode:ByNavigationItemModeImage position:ByNavigationItemPositionModeRight];
    self.tintColor = [UIColor whiteColor];
    [self createShowHeader];
    [self createTableView];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self setNaviAttributeWithTranslucent:YES stateBarLightContent:YES];
    self.navigationController.navigationBar.translucent = YES;
    // 设置导航栏标题颜色和字体大小
    NSDictionary *attr = @{
                           NSForegroundColorAttributeName:[UIColor whiteColor],
                           NSFontAttributeName:B_FONT(18)
                           };
    [self.navigationController.navigationBar setTitleTextAttributes:attr];
    
    ByNetUserModel *user = BY_USER;
    if (user == nil) {
        _isLogin = NO;
    }else{
        _isLogin = YES;
    }
    
    
    if (_isLogin == NO) {
        [self.showHeader.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self.showHeader addSubview:self.headerView];
    }else{
        [self.showHeader.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self.showHeader addSubview:self.logoutHV];;
    }
    
    // 如果登陆了，则要设置用户头像
    if (user != nil) {
        self.headerView.nickLabel.text = user.nickname;
        [self.headerView.iconImgView setImageWithURL:[NSURL URLWithString:user.head] placeholderImage:DEFAULT_HEAD];
        self.headerView.mobileLabel.text = user.mobile;
    }
}


- (void)createShowHeader {
    
    self.showHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH * (400.0 / 750))];
    [self.view addSubview:self.showHeader];
}


// 创建表视图
- (void)createTableView {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, self.showHeader.bottom, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - self.showHeader.height) style:UITableViewStylePlain];
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.backgroundColor = TABLE_COLOR;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    ByCoreRefreshGifHeader *t_header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
//        [self updateUserInfo];
    }];
    self.table.mj_header = t_header;
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _titles.count;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *subTitles = _titles[section];
    return subTitles.count;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.textLabel.font = FONT(14);
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = ZJ_SEPLINE_COLOR;
        line.width = SCREEN_WIDTH;
        line.height = 0.5;
        line.left = 0;
        line.bottom = 44;
        [cell.contentView addSubview:line];
    }
    NSArray *subImages = _images[indexPath.section];
    cell.imageView.image = [UIImage imageNamed:subImages[indexPath.row]];
    NSArray *subTitles = _titles[indexPath.section];
    cell.textLabel.text = subTitles[indexPath.row];
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}
// 设置组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
//    if (_isLogin == NO) {
//        ByLoginVC *vc = [[ByLoginVC alloc] init];
//        ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
//        [self presentViewController:navi animated:YES completion:nil];
//        return;
//    }
    
}


#pragma mark - 点击
- (void)rightItemsAction:(NSInteger)rightIndex {
    
//    if (_isLogin == NO) {
//        ByLoginVC *vc = [[ByLoginVC alloc] init];
//        ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
//        [self presentViewController:navi animated:YES completion:nil];
//        return;
//    }
    
    ByMineSettingVC *vc = [[ByMineSettingVC alloc] init];
//    vc.refreshBlock = ^(){
//        NSLog(@"推出");
//    };
    [self by_normalPush:vc hasNavi:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
